<template>
  <div id="login-page">
    <div class="login-box">
      <div class="avatar">
        <img src="../assets/avatar.jpg" alt="" />
      </div>
      <el-form class="login-form" :model="loginForm" :rules="rules" ref="loginForm">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" type="password"
            v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="onSubmit('loginForm')">登录</el-button>
          <el-button type="info" @click="onReset('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    // this.$nextTick().then(value => {
    //   console.log(value.$el)
    // })
  },

  methods: {
    onSubmit(formName) {
      // console.log(this.$refs[formName].validate())
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        const data = await this.$axios.post('/login', this.loginForm)
        if (data.meta.status !== 200) return this.$message.error('用户名或密码错误！')
        this.$message.success('登录成功')
        sessionStorage.setItem('token', data.data.token)
        this.$router.push('/home')
      })
    },
    onReset(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped lang='less'>
#login-page {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-box {
    position: relative;
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 5px;
    .avatar {
      box-sizing: border-box;
      position: absolute;
      top: -65px;
      left: 50%;
      transform: translateX(-50%);
      width: 130px;
      height: 130px;
      background: #fff;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;
      box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
      border: 10px solid #fff;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .login-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
